﻿import { Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {  ComboboxItemDto, LibraryOfLaborServiceProxy, ProductionOutput} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import * as moment from 'moment';
import { from } from 'rxjs';

@Component({
    selector: 'createOrEditLibraryModal',
    templateUrl: './create-or-edit-library-modal.component.html'
})
export class CreateOrEditLibraryModalComponent extends AppComponentBase {

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @ViewChild('createOrEditLibraryModal') modal: ModalDirective;

    isNormalProduction = true;
    productionOutput :ProductionOutput = new ProductionOutput();
    active = false;
    saving = false;

  
    constructor(
        injector: Injector,
        private _libraryOfLaborServiceProxy: LibraryOfLaborServiceProxy
    ) {
        super(injector);
    }

    showAddNormal(productionId?: string):void{
        this.active = true;
        this.modal.show();
        this.isNormalProduction = true;
        if(productionId == null)
        {
            this.productionOutput = new ProductionOutput();
        } else
        {
            this._libraryOfLaborServiceProxy.getNormalProduction(productionId).subscribe((result) =>
            {
                this.productionOutput = result;
            });
        }            
    }

    showAddSpecial(productionId?: string):void{
        this.active = true;
        this.modal.show();
        this.isNormalProduction = false;
        if(productionId == null)
        {
            this.productionOutput = new ProductionOutput();
        } else
        {
            this._libraryOfLaborServiceProxy.getSpecialProduction(productionId).subscribe((result) =>
            {
                this.productionOutput = result;
            });
        }            
    }

    saveProduction():void{
        this.saving = true;
        if(this.productionOutput.id == null || this.productionOutput.id == ''){
            this._libraryOfLaborServiceProxy.createProduction(this.productionOutput).subscribe((result) =>
            {
                this.notify.info(this.l('SavedSuccessfully'));
                this.saving = false;
                this.close();
                this.modalSave.emit(null);
            });
        }else
        {
            this._libraryOfLaborServiceProxy.updateProduction(this.productionOutput).subscribe((result) =>
            {
                
                this.notify.info(this.l('SavedSuccessfully'));
                this.saving = false;
                this.close();
                this.modalSave.emit(null);
            });
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
